package com.ruoyi.order.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.enums.TopOrderStatusEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.MesTopOrderMapper;
import com.ruoyi.order.domain.MesTopOrder;
import com.ruoyi.order.service.IMesTopOrderService;

/**
 * 生产计划订单Service业务层处理
 * 
 * @author guoxl
 * @date 2025-05-31
 */
@Service
public class MesTopOrderServiceImpl implements IMesTopOrderService 
{
    @Autowired
    private MesTopOrderMapper mesTopOrderMapper;

    /**
     * 查询生产计划订单
     * 
     * @param orderId 生产计划订单主键
     * @return 生产计划订单
     */
    @Override
    public MesTopOrder selectMesTopOrderByOrderId(Long orderId)
    {
        return mesTopOrderMapper.selectMesTopOrderByOrderId(orderId);
    }

    /**
     * 查询生产计划订单
     *
     * @return 生产中订单
     */
    @Override
    public MesTopOrder selectMesTopOrderProducting()
    {
        MesTopOrder param = new MesTopOrder();
        param.setStatus(TopOrderStatusEnum.PRODUCING.getCode());
        List<MesTopOrder> orders = mesTopOrderMapper.selectMesTopOrderList(param);
        if (CollectionUtils.isNotEmpty(orders)){
            return orders.get(0);
        }
        return  null;
    }

    /**
     * 查询生产计划订单列表
     * 
     * @param mesTopOrder 生产计划订单
     * @return 生产计划订单
     */
    @Override
    public List<MesTopOrder> selectMesTopOrderList(MesTopOrder mesTopOrder)
    {
        return mesTopOrderMapper.selectMesTopOrderList(mesTopOrder);
    }

    /**
     * 获取正在生产的订单
     * @return
     */
    public MesTopOrder getProducingOrder(){
        MesTopOrder mesTopOrder = new MesTopOrder();
        mesTopOrder.setStatus(TopOrderStatusEnum.PRODUCING.getCode());
        List<MesTopOrder> mesTopOrders = mesTopOrderMapper.selectMesTopOrderList(mesTopOrder);
        if (mesTopOrders!=null && mesTopOrders.size() > 0){
            return mesTopOrders.get(0);
        }
        return null;
    }

    /**
     * 新增生产计划订单
     * 
     * @param mesTopOrder 生产计划订单
     * @return 结果
     */
    @Override
    public int insertMesTopOrder(MesTopOrder mesTopOrder)
    {
        mesTopOrder.setCreateTime(DateUtils.getNowDate());
        mesTopOrder.setStatus(TopOrderStatusEnum.BEFORE_PRODUCE.getCode());
        return mesTopOrderMapper.insertMesTopOrder(mesTopOrder);
    }

    /**
     * 修改生产计划订单
     * 
     * @param mesTopOrder 生产计划订单
     * @return 结果
     */
    @Override
    public int updateMesTopOrder(MesTopOrder mesTopOrder)
    {
        mesTopOrder.setUpdateTime(DateUtils.getNowDate());
        return mesTopOrderMapper.updateMesTopOrder(mesTopOrder);
    }

    /**
     * 批量删除生产计划订单
     * 
     * @param orderIds 需要删除的生产计划订单主键
     * @return 结果
     */
    @Override
    public int deleteMesTopOrderByOrderIds(Long[] orderIds)
    {
        return mesTopOrderMapper.deleteMesTopOrderByOrderIds(orderIds);
    }

    /**
     * 删除生产计划订单信息
     * 
     * @param orderId 生产计划订单主键
     * @return 结果
     */
    @Override
    public int deleteMesTopOrderByOrderId(Long orderId)
    {
        return mesTopOrderMapper.deleteMesTopOrderByOrderId(orderId);
    }

    /**
     * 生产订单
     * @param id
     * @return
     */
    @Override
    public int testingOrderById(Long id) {
        MesTopOrder mesTopOrder = mesTopOrderMapper.selectMesTopOrderByOrderId(id);
        if (mesTopOrder.getStatus().equals(TopOrderStatusEnum.BEFORE_PRODUCE.getCode())){
            mesTopOrder.setStatus(TopOrderStatusEnum.PRODUCING.getCode());
            return mesTopOrderMapper.updateMesTopOrder(mesTopOrder);
        }
        return 0;
    }
    /**
     * 生产订单
     * @param id
     * @return
     */
    @Override
    public int completeOrderById(Long id) {
        MesTopOrder mesTopOrder = mesTopOrderMapper.selectMesTopOrderByOrderId(id);
        if (mesTopOrder.getStatus().equals(TopOrderStatusEnum.PRODUCING.getCode())){
            mesTopOrder.setStatus(TopOrderStatusEnum.PRODUCED.getCode());
            return mesTopOrderMapper.updateMesTopOrder(mesTopOrder);
        }
        return 0;
    }
}
